package cn.tedu.product.controller;


import cn.tedu.product.base.response.JsonResult;
import cn.tedu.product.base.response.StatusCode;
import cn.tedu.product.mapper.UserMapper;
import cn.tedu.product.pojo.dto.UserLoginDTO;
import cn.tedu.product.pojo.dto.UserRegDTO;
import cn.tedu.product.pojo.entity.User;
import cn.tedu.product.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("v1/users/")
@Api(tags = "用户模块")
public class UserController {
    @Autowired(required = false)
    private UserMapper  userMapper;

    @PostMapping("reg")
    @ApiOperation(value="注册")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
        UserVO userVO=userMapper.selectByUname(userRegDTO.getUsername());
        if (userVO==null){
            User user=new User();
            BeanUtils.copyProperties(userRegDTO,user);
            int i = userMapper.insertUser(user);
            if (i==1)
                return JsonResult.ok();
            return JsonResult.notok();
        }
        return new JsonResult(StatusCode.USERNAME_EXITS);
    }


    @PostMapping("login")
    @ApiOperation(value = "登录")
    public JsonResult login(@RequestBody @Validated UserLoginDTO loginDTO,@ApiIgnore HttpSession session){
        UserVO userVO=userMapper.selectByUname(loginDTO.getUsername());
        if (userVO!=null){
            if (userVO.getPassword().equals(loginDTO.getPassword())){
                if (userVO.getIsAdmin()==0){
                    session.setAttribute("user",userVO);
                    return JsonResult.ok();
                }
            }
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
        return new JsonResult(StatusCode.USERNAME_ERROR);
    }



    @PostMapping("deleteMy")
    @ApiOperation(value = "注销账号")
    public JsonResult deleteMy(@ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        int i = userMapper.deleteUser(userVO.getUsername());
        if (i==1)
            return JsonResult.ok("注销成功！");
        return JsonResult.notok("系统繁忙~");
    }

    @PostMapping("logout")
    @ApiOperation(value = "退出登录")
    public JsonResult logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
        return JsonResult.ok();
    }



    @PostMapping("currentUser")
    @ApiOperation(value = "获取当前用户")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        return JsonResult.ok(session.getAttribute("user"));
    }




}
